///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_ZZ_ZACTION_H_
#define _BUOLA_ZZ_ZACTION_H_

#include <buola/zz/zbase.h>

namespace buola { namespace zz {

namespace detail {
    
    template<typename tF,typename tA,typename tB,typename tC>
    auto call_function(const tF &pF,tA &&pA,tB &&pB,tC &&pC) -> decltype(pF(std::forward<tA>(pA),std::forward<tB>(pB),std::forward<tC>(pC)))
    {
        return pF(pA,pB,pC);
    }

    template<typename tF,typename tA,typename tB,typename tC>
    auto call_function(const tF &pF,tA &&pA,tB &&pB,tC&&) -> decltype(pF(std::forward<tA>(pA),std::forward<tB>(pB)))
    {
        return pF(pA,pB);
    }

    template<typename tF,typename tA,typename tB,typename tC>
    auto call_function(const tF &pF,tA &&pA,tB&&,tC&&) -> decltype(pF(std::forward<tA>(pA)))
    {
        return pF(pA);
    }

    template<typename tF,typename tA,typename tB,typename tC>
    auto call_function(const tF &pF,tA&&,tB&&,tC&&) -> decltype(pF())
    {
        return pF();
    }

/*namespace detail*/ }

template<typename tA,typename tF,bool vAttr>
class ZAction : public ZBase<ZAction<tA,tF,vAttr>>
{
public:
    template<typename tIt>
    struct tAttribute
    {
        typedef typename std::conditional<vAttr,typename tA::template tAttribute<tIt>::tType,UUnused>::type tType;
    };

    ZAction(const ZAction &pRH)
        :   mA(pRH.mA)
        ,   mF(pRH.mF)
    {}
    
    ZAction(ZAction &&pRH)
        :   mA(std::move(pRH.mA))
        ,   mF(std::move(pRH.mF))
    {}
    
    template<typename tPA,typename tPF>
    explicit ZAction(tPA &&pA,tPF &&pF)
        :   mA(std::forward<tPA>(pA))
        ,   mF(std::forward<tPF>(pF))
    {}
    
    template<typename tIt,typename tAttr,typename tCtx,typename tSk>
    bool Parse(tIt &pB,const tIt &pE,tAttr &pAttr,tCtx &pCtx,const tSk &pSk) const
    {
        typename tAttribute<tIt>::tType lAttr;
        
        if(!mA.Parse(pB,pE,lAttr,pCtx,pSk)) return false;
        bool lPass=true;
        detail::call_function(mF,lAttr,pCtx,lPass);
        if(!lPass) return false;
        
        pAttr=lAttr;
    
        return true;
    }

private:    
    tA mA;
    tF mF;
};

template<typename tA>
class CActionCreator
{
public:
    CActionCreator(tA &&pA)
        :   mA((tA&&)pA)
    {}
    
    template<typename tF>
    ZAction<ODecay<tA>,ODecay<tF>,true>
    operator[](tF &&pF)
    {
        return ZAction<ODecay<tA>,ODecay<tF>,true>(std::forward<tA>(mA),std::forward<tF>(pF));
    }
    
    template<typename tF>
    ZAction<ODecay<tA>,ODecay<tF>,false>
    operator()(tF &&pF)
    {
        return ZAction<ODecay<tA>,ODecay<tF>,false>(std::forward<tA>(mA),std::forward<tF>(pF));
    }

    tA &&mA;
};
    
/*namespace zz*/ } /*namespace buola*/ }

#endif
